//
//  ViewController.m
//  UI20-XML
//
//  Created by 王道林 on 16/8/10.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ViewController.h"
#import "MovieClass.h"
#import "GDataXMLNode.h"
@interface ViewController ()<NSXMLParserDelegate>
@property (nonatomic, strong) NSMutableArray *arrDataSource;
@property (nonatomic, copy) NSString *element;
@property (nonatomic, copy) NSString *page;
@property (nonatomic, copy) NSString *pageValue;

@property (nonatomic, copy) NSString *elenment;
@property (nonatomic, strong) NSMutableArray *arrDom;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.arrDom = [NSMutableArray array];
}
- (IBAction)xml解析:(id)sender {
    NSString *string = [[NSBundle mainBundle] pathForResource:@"XML" ofType:@"xml"];
    NSData *data = [NSData dataWithContentsOfFile:string];
    NSXMLParser *xml = [[NSXMLParser alloc] initWithData:data];
    xml.delegate = self;
    [xml parse];
}
- (void)parserDidStartDocument:(NSXMLParser *)parser {
    self.arrDataSource = [NSMutableArray array];
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
    NSLog(@"%@", _arrDataSource);
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict {
    _elenment = elementName;
    if ([_elenment isEqualToString:@"AlbumInfo"]) {
        MovieClass *movies = [MovieClass new];
        [self.arrDataSource addObject:movies];
    }
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    if (_elenment) {
        MovieClass *movies = [_arrDataSource lastObject];
        [movies setValue:string forKey:_elenment];
    }
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
    NSLog(@"报错原因：%@", parseError);
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    _elenment = nil;
   
    
}


- (IBAction)dom解析:(id)sender {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"XML" ofType:@"xml"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil];
    GDataXMLElement *root = document.rootElement;
    NSArray *rootElement = root.children;
//    NSLog(@"%@",rootElement);
    for (GDataXMLElement *childElement in rootElement) {
        _element = childElement.name;
        NSLog(@"%@", childElement.children);
        NSLog(@"%ld",childElement.children.count);
        for (GDataXMLElement *sunElement in childElement.children) {
            
            MovieClass *movies = [MovieClass new];
            if ([_element isEqualToString:@"page"]) {
                [movies setValue:sunElement.stringValue forKey:sunElement.name];
            }
            [self.arrDom addObject:movies];
            for (GDataXMLElement *grandElement in sunElement.children) {
                [movies setValue:grandElement.stringValue forKey:grandElement.name];
            }
        }
    }
//    NSLog(@"%@", _arrDom);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
